package com.glut.service.impl;

import com.github.pagehelper.Page;
import com.glut.mapper.SdfsQuestionsMapper;
import com.glut.service.SdfsQuestionsService;
import com.glut.vo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author:沈林玉
 * @date:2020/2/14 8:55
 * @Description:
 */
@Service("sdfsQuestionsService")
public class SdfsQuestionsServiceImpl implements SdfsQuestionsService {

    @Resource
    SdfsQuestionsMapper sdfsQuestionsMapper;

    /**
     * 1.查询总试题总记录数
     * @return
     */
    @Override
    public int selectSdfsCount() {
        return sdfsQuestionsMapper.selectSdfsCount();
    }

    /**
     * 2.查询单选题总记录数
     * @return
     */
    @Override
    public int selectSQCount(SQuestionDetail sQuestionDetail) {
        return sdfsQuestionsMapper.selectSQCount(sQuestionDetail);
    }

    /**
     * 3.查询判断题总记录数
     * @return
     */
    @Override
    public int selectDQCount(DQuestionDetail dQuestionDetail) {
        return sdfsQuestionsMapper.selectDQCount(dQuestionDetail);
    }

    /**
     * 4.查询填空题总记录数
     * @return
     */
    @Override
    public int selectFQCount(FQuestionDetail fQuestionDetail) {
        return sdfsQuestionsMapper.selectFQCount(fQuestionDetail);
    }

    /**
     * 5.查询简答综合题总记录数
     * @return
     */
    @Override
    public int selectShQCount(ShQuestionDetail shQuestionDetail) {
        return sdfsQuestionsMapper.selectShQCount(shQuestionDetail);
    }

    /**
     * 1.查询总试题
     * @param
     * @return
     */
    @Override
    public List<SdfsQuestions> selectAllSdfsQuestions(String kp) {
        return sdfsQuestionsMapper.selectAllSdfsQuestions(kp);
    }

    /**
     * 1.查询总试题
     * @param
     * @return
     */
    @Override
    public List<SdfsQuestions> selectAllSdfsQuestionsByKld(String kp) {
        return sdfsQuestionsMapper.selectAllSdfsQuestionsByKld(kp);
    }


    /**
     * 1.查询总试题添加题型限制
     * @param
     * @return
     */
    public List<SdfsQuestions> selectAllSdfsQuestionsByType(Integer questionType){
        return sdfsQuestionsMapper.selectAllSdfsQuestionsByType(questionType);
    }

    /**
     * 2.查询单选题
     * @param sQ
     * @return
     */
    @Override
    public Page<SQuestionDetail> selectAllSQuestions(@Param("sQ") SQuestionDetail sQ,
                                                     @Param("pageNum") int pageNum,
                                                     @Param("pageSize") int pageSize) {
        return sdfsQuestionsMapper.selectAllSQuestions(sQ,pageNum,pageSize);
    }

    /**
     * 3.通过ID查询单选题
     * @param sQuestionDetail
     * @return
     */
    @Override
    public SQuestionDetail selectSQuestionsByID(SQuestionDetail sQuestionDetail) {
        return sdfsQuestionsMapper.selectSQuestionsByID(sQuestionDetail);
    }

    /**
     * 4.查询判断题
     * @param dQ
     * @return
     */
    @Override
    public Page<DQuestionDetail> selectAllDQuestions(@Param("dQ")DQuestionDetail dQ,
                                                     @Param("pageNum") int pageNum,
                                                     @Param("pageSize") int pageSize) {
        return sdfsQuestionsMapper.selectAllDQuestions(dQ,pageNum,pageSize);
    }

    /**
     * 5.通过ID查询判断题
     * @param dQuestionDetail
     * @return
     */
    @Override
    public DQuestionDetail selectDQuestionsByID(DQuestionDetail dQuestionDetail) {
        return sdfsQuestionsMapper.selectDQuestionsByID(dQuestionDetail);
    }

    /**
     * 6.查询填空题
     * @param fQ
     * @return
     */
    @Override
    public Page<FQuestionDetail> selectAllFQuestions(@Param("fQ")FQuestionDetail fQ,
                                                     @Param("pageNum") int pageNum,
                                                     @Param("pageSize") int pageSize) {
        return sdfsQuestionsMapper.selectAllFQuestions(fQ,pageNum,pageSize);
    }

    /**
     * 7.通过ID查询填空题
     * @param fQuestionDetail
     * @return
     */
    @Override
    public FQuestionDetail selectFQuestionsByID(FQuestionDetail fQuestionDetail) {
        return sdfsQuestionsMapper.selectFQuestionsByID(fQuestionDetail);
    }

    /**
     * 8.查询简答综合题
     * @param shQ
     * @return
     */
    @Override
    public Page<ShQuestionDetail> selectAllShQuestions(@Param("shQ")ShQuestionDetail shQ,
                                                       @Param("pageNum") int pageNum,
                                                       @Param("pageSize") int pageSize) {
        return sdfsQuestionsMapper.selectAllShQuestions(shQ,pageNum,pageSize);
    }

    /**
     * 9.通过ID查询简答综合题
     * @param shQuestionDetail
     * @return
     */
    @Override
    public ShQuestionDetail selectShQuestionsByID(ShQuestionDetail shQuestionDetail) {
        return sdfsQuestionsMapper.selectShQuestionsByID(shQuestionDetail);
    }

    @Override
    public List<SdfsQuestions> checkAllQuesByKpsAndQtAndDl(List<String> KnowPoints, Integer questionType, Double qDiffiCultyLevel) {
        return null;
    }

    @Override
    public List<SdfsQuestions> checkAllQuesByKpsAndQtAndDl(List<String> KnowPoints, Integer questionType, Double qDiffiCultyLevel, int pageNum, int pageSize) {
        return null;
    }

    @Override
    public int countQuesByKpsAndQtAndDl(List<String> KnowPoints, Integer questionType, Double qDiffiCultyLevel) {
        return 0;
    }

    @Override
    public List<SdfsQuestions> checkAllQuesByKpsAndQtAndDl() {

        SdfsQuestions sdfsQuestions = new SdfsQuestions();
        List<SdfsQuestions> sdfsQuestionsList = new ArrayList<>();

        // 先不进行知识点限制题目
        //if(konwPoints!=null) {
            //for (String kp:konwPoints){
                List<SdfsQuestions> sdfsQuestions1 = sdfsQuestionsMapper.selectAllSdfsQuestions(null);
                for (SdfsQuestions s:sdfsQuestions1){
                    sdfsQuestionsList.add(s);
                }
            //}
        //}
        return sdfsQuestionsList;
    }

    @Override
    public int countgetBy(String qId) {
        return 0;
    }


    @Override
    public List<SdfsQuestions> getQuestionsByEIdAndEQTId(Long eId, Long eqtId, int eType) {
        return null;
    }

    /**
     * 1.新增总试题
     * @param sdfsQuestions
     * @return
     */
    @Override
    public int InsertSdfsQuestions(SdfsQuestions sdfsQuestions) {
        System.out.println(sdfsQuestions);
        return sdfsQuestionsMapper.InsertSdfsQuestions(sdfsQuestions);
    }

    /**
     * 2.查找最近插入的试题ID
     * @param sdfsQuestions
     * @return
     */
    @Override
    public String selectQuestionsId(SdfsQuestions sdfsQuestions) {
        return sdfsQuestionsMapper.selectQuestionsId(sdfsQuestions);
    }

    /**
     * 3.新增单选题
     * @param sQuestionDetail
     * @return
     */
    @Override
    @Transactional
    public int insertSQuestionDetail(SQuestionDetail sQuestionDetail) {
        int s = this.InsertSdfsQuestions(sQuestionDetail.getSdfsQuestions());
        String newID = sQuestionDetail.getSdfsQuestions().getNewID();
        //String questionsId = sdfsQuestionsMapper.selectQuestionsId(sQuestionDetail.getSdfsQuestions());
        sQuestionDetail.setsQuestionID(newID);
        System.out.println("主键："+newID);
        System.out.println(sQuestionDetail);
        int sQuestionDetailInsert = sdfsQuestionsMapper.insertSQuestionDetail(sQuestionDetail);
        return sQuestionDetailInsert;
    }

    /**
     * 4.新增判断题
     * @param dQuestionDetail
     * @return
     */
    @Override
    @Transactional
    public int insertDQuestionDetail(DQuestionDetail dQuestionDetail) {
        int s = this.InsertSdfsQuestions(dQuestionDetail.getSdfsQuestions());
        String newID = dQuestionDetail.getSdfsQuestions().getNewID();
        //String questionsId = sdfsQuestionsMapper.selectQuestionsId(sQuestionDetail.getSdfsQuestions());
        dQuestionDetail.setdQuestionID(newID);
        System.out.println("主键："+newID);
        System.out.println(dQuestionDetail);
        int dQuestionDetailInsert = sdfsQuestionsMapper.insertDQuestionDetail(dQuestionDetail);
        return dQuestionDetailInsert;
    }

    /**
     * 5.新增填空题
     * @param fQuestionDetail
     * @return
     */
    @Override
    @Transactional
    public int insertFQuestionDetail(FQuestionDetail fQuestionDetail) {
        int f = this.InsertSdfsQuestions(fQuestionDetail.getSdfsQuestions());
        String newID = fQuestionDetail.getSdfsQuestions().getNewID();
        //String newID = sdfsQuestionsMapper.selectQuestionsId(fQuestionDetail.getSdfsQuestions());
        fQuestionDetail.setfQuestionID(newID);
        System.out.println("主键："+newID);
        System.out.println(fQuestionDetail);
        int fQuestionDetailInsert = sdfsQuestionsMapper.insertFQuestionDetail(fQuestionDetail);
        return fQuestionDetailInsert;
    }

    /**
     * 6.新增简答综合题
     * @param shQuestionDetail
     * @return
     */
    @Override
    @Transactional
    public int insertShQuestionDetail(ShQuestionDetail shQuestionDetail) {
        int sh = this.InsertSdfsQuestions(shQuestionDetail.getSdfsQuestions());
        String newID = shQuestionDetail.getSdfsQuestions().getNewID();
        // String newID = sdfsQuestionsMapper.selectQuestionsId(shQuestionDetail.getSdfsQuestions());
        shQuestionDetail.setShQuestionID(newID);
        System.out.println("主键："+newID);
        System.out.println(shQuestionDetail);
        int shQuestionDetailInsert = sdfsQuestionsMapper.insertShQuestionDetail(shQuestionDetail);
        return shQuestionDetailInsert;
    }

    /**
     * 7.批量导入试题
     * @param list
     * @return
     */
    @Override
    @Transactional
    public int saveBatchImportQuestion(List<Object> list,Integer questionType) {

        int count = 0;
            if (questionType==1){
                List<SQuestionDetail> insertSQ = new ArrayList<SQuestionDetail>();
                Iterator<Object> iterator = list.iterator();
                while (iterator.hasNext()) {
                    SQuestionDetail sQuestionDetail = (SQuestionDetail) iterator.next();
                    insertSQ.add(sQuestionDetail);
                }

                for (SQuestionDetail sQuestionDetail : insertSQ) {
                    count += this.insertSQuestionDetail(sQuestionDetail);;
                }

            }else if (questionType==2){
                List<DQuestionDetail> insertDQ = new ArrayList<DQuestionDetail>();
                Iterator<Object> iterator = list.iterator();
                while (iterator.hasNext()) {
                    DQuestionDetail dQuestionDetail = (DQuestionDetail) iterator.next();
                    insertDQ.add(dQuestionDetail);
                }

                for (DQuestionDetail dQuestionDetail : insertDQ) {
                    count += this.insertDQuestionDetail(dQuestionDetail);;
                }

            }else if (questionType==3){
                List<FQuestionDetail> insertFQ = new ArrayList<FQuestionDetail>();
                Iterator<Object> iterator = list.iterator();
                while (iterator.hasNext()) {
                    FQuestionDetail fQuestionDetail = (FQuestionDetail) iterator.next();
                    insertFQ.add(fQuestionDetail);
                }

                for (FQuestionDetail fQuestionDetail : insertFQ) {
                    count += this.insertFQuestionDetail(fQuestionDetail);;
                }

            }else if (questionType==4){
                List<ShQuestionDetail> insertShQ = new ArrayList<ShQuestionDetail>();
                Iterator<Object> iterator = list.iterator();
                while (iterator.hasNext()) {
                    ShQuestionDetail shQuestionDetail = (ShQuestionDetail) iterator.next();
                    insertShQ.add(shQuestionDetail);
                }

                for (ShQuestionDetail shQuestionDetail : insertShQ) {
                    count += this.insertShQuestionDetail(shQuestionDetail);;
                }

            }else if (questionType==5){
                for(Object o:list){
                    //DQuestionDetail dQuestionDetail = (DQuestionDetail) o;
                    //this.insertDQuestionDetail(dQuestionDetail);
                }
            }

        return count;
    }

    /**
     * 1.更新单选题
     * @param map
     */
    @Override
    public void updateSQuestions(Map<String, Object> map) {
        this.sdfsQuestionsMapper.updateSQuestions(map);
    }

    /**
     * 2.更新判断、填空、简单、综合题
     * @param map
     */
    @Override
    public void updatedfshQuestions(Map<String, Object> map) {
        this.sdfsQuestionsMapper.updatedfshQuestions(map);
    }

    /**
     * 1.删除单、判、填、简综试题
     * @param map
     */
    @Override
    public void deleteSdfsQuestions(Map<String, Object> map) {
        this.sdfsQuestionsMapper.deleteSdfsQuestions(map);
    }
}
